<template>
  <div>
    <el-upload
      action=""
      accept=".xlsx"
      :limit="1"
      :before-upload="handleFileUploaded"
    >
      <el-button type="primary" plain>选择文件</el-button>
    </el-upload>
    <div slot="tip" class="el-upload__tip">
      只能上传xlsx/xls文件，且不超过5MB
    </div>
  </div>
</template>

<script>
import * as XLSX from "xlsx";

export default {
  data() {
    return {
      tableData: [],
      dict: {
        0: "00:00:00",
        1: "00:15:00",
        2: "00:30:00",
        3: "00:45:00",
        4: "01:00:00",
        5: "01:15:00",
        6: "01:30:00",
        7: "01:45:00",
        8: "02:00:00",
        9: "02:15:00",
        10: "02:30:00",
        11: "02:45:00",
        12: "03:00:00",
        13: "03:15:00",
        14: "03:30:00",
        15: "03:45:00",
        16: "04:00:00",
        17: "04:15:00",
        18: "04:30:00",
        19: "04:45:00",
        20: "05:00:00",
        21: "05:15:00",
        22: "05:30:00",
        23: "05:45:00",
        24: "06:00:00",
        25: "06:15:00",
        26: "06:30:00",
        27: "06:45:00",
        28: "07:00:00",
        29: "07:15:00",
        30: "07:30:00",
        31: "07:45:00",
        32: "08:00:00",
        33: "08:15:00",
        34: "08:30:00",
        35: "08:45:00",
        36: "09:00:00",
        37: "09:15:00",
        38: "09:30:00",
        39: "09:45:00",
        40: "10:00:00",
        41: "10:15:00",
        42: "10:30:00",
        43: "10:45:00",
        44: "11:00:00",
        45: "11:15:00",
        46: "11:30:00",
        47: "11:45:00",
        48: "12:00:00",
        49: "12:15:00",
        50: "12:30:00",
        51: "12:45:00",
        52: "13:00:00",
        53: "13:15:00",
        54: "13:30:00",
        55: "13:45:00",
        56: "14:00:00",
        57: "14:15:00",
        58: "14:30:00",
        59: "14:45:00",
        60: "15:00:00",
        61: "15:15:00",
        62: "15:30:00",
        63: "15:45:00",
        64: "16:00:00",
        65: "16:15:00",
        66: "16:30:00",
        67: "16:45:00",
        68: "17:00:00",
        69: "17:15:00",
        70: "17:30:00",
        71: "17:45:00",
        72: "18:00:00",
        73: "18:15:00",
        74: "18:30:00",
        75: "18:45:00",
        76: "19:00:00",
        77: "19:15:00",
        78: "19:30:00",
        79: "19:45:00",
        80: "20:00:00",
        81: "20:15:00",
        82: "20:30:00",
        83: "20:45:00",
        84: "21:00:00",
        85: "21:15:00",
        86: "21:30:00",
        87: "21:45:00",
        88: "22:00:00",
        89: "22:15:00",
        90: "22:30:00",
        91: "22:45:00",
        92: "23:00:00",
        93: "23:15:00",
        94: "23:30:00",
        95: "23:45:00",
      },
      reverse_dict: {
        "00:00:00": 0,
        "00:15:00": 1,
        "00:30:00": 2,
        "00:45:00": 3,
        "01:00:00": 4,
        "01:15:00": 5,
        "01:30:00": 6,
        "01:45:00": 7,
        "02:00:00": 8,
        "02:15:00": 9,
        "02:30:00": 10,
        "02:45:00": 11,
        "03:00:00": 12,
        "03:15:00": 13,
        "03:30:00": 14,
        "03:45:00": 15,
        "04:00:00": 16,
        "04:15:00": 17,
        "04:30:00": 18,
        "04:45:00": 19,
        "05:00:00": 20,
        "05:15:00": 21,
        "05:30:00": 22,
        "05:45:00": 23,
        "06:00:00": 24,
        "06:15:00": 25,
        "06:30:00": 26,
        "06:45:00": 27,
        "07:00:00": 28,
        "07:15:00": 29,
        "07:30:00": 30,
        "07:45:00": 31,
        "08:00:00": 32,
        "08:15:00": 33,
        "08:30:00": 34,
        "08:45:00": 35,
        "09:00:00": 36,
        "09:15:00": 37,
        "09:30:00": 38,
        "09:45:00": 39,
        "10:00:00": 40,
        "10:15:00": 41,
        "10:30:00": 42,
        "10:45:00": 43,
        "11:00:00": 44,
        "11:15:00": 45,
        "11:30:00": 46,
        "11:45:00": 47,
        "12:00:00": 48,
        "12:15:00": 49,
        "12:30:00": 50,
        "12:45:00": 51,
        "13:00:00": 52,
        "13:15:00": 53,
        "13:30:00": 54,
        "13:45:00": 55,
        "14:00:00": 56,
        "14:15:00": 57,
        "14:30:00": 58,
        "14:45:00": 59,
        "15:00:00": 60,
        "15:15:00": 61,
        "15:30:00": 62,
        "15:45:00": 63,
        "16:00:00": 64,
        "16:15:00": 65,
        "16:30:00": 66,
        "16:45:00": 67,
        "17:00:00": 68,
        "17:15:00": 69,
        "17:30:00": 70,
        "17:45:00": 71,
        "18:00:00": 72,
        "18:15:00": 73,
        "18:30:00": 74,
        "18:45:00": 75,
        "19:00:00": 76,
        "19:15:00": 77,
        "19:30:00": 78,
        "19:45:00": 79,
        "20:00:00": 80,
        "20:15:00": 81,
        "20:30:00": 82,
        "20:45:00": 83,
        "21:00:00": 84,
        "21:15:00": 85,
        "21:30:00": 86,
        "21:45:00": 87,
        "22:00:00": 88,
        "22:15:00": 89,
        "22:30:00": 90,
        "22:45:00": 91,
        "23:00:00": 92,
        "23:15:00": 93,
        "23:30:00": 94,
        "23:45:00": 95,
      },
    };
  },
  methods: {
    handleFileUploaded(file) {
      console.log("==============>>开始解析excel文件<<==============");
      let reader = new FileReader();
      let _this = this;
      reader.onload = (e) => {
        const data = new Uint8Array(e.target.result);
        const workbook = XLSX.read(data, {
          type: "array",
          raw: true,
          cellDates: true,
        });
        const worksheet = workbook.Sheets[workbook.SheetNames[0]];
        const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
        _this.tableSize = jsonData.length;
        const regex =
          /^[a-zA-Z]{3} [a-zA-Z]{3} \d{2} \d{4} \d{2}:\d{2}:\d{2} GMT\+\d{4} \(中国标准时间\)$/;
        const tableData = jsonData.map((row) => {
          return row.map((cell) => {
            if (cell !== "" || cell !== null) {
              if (regex.test(cell.toString())) {
                let date = new Date(cell).getTime() + 44 * 1000;
                return _this.formatDateByProp(date);
              }
            }
            return cell;
          });
        });
        // 处理数据
        let tableMap = {};
        let resultData = [];
        let header = tableData.shift();
        tableData.forEach((item) => {
          let key = _this.formatDateByProp(item[0], "yyyy-MM-dd");
          let index =
            _this.reverse_dict[_this.formatDateByProp(item[0], "hh:mm:ss")];
          if (!tableMap[key]) {
            tableMap[key] = new Array(96).fill(null);
          }
          tableMap[key][index] = item;
        });
        resultData = JSON.parse(JSON.stringify(tableData));
        for (let i = 1; i <= tableData.length - 1; i++) {
          let lastData = tableData[i - 1];
          let currData = tableData[i];
          let lastTimeIndex =
            _this.reverse_dict[_this.formatDateByProp(lastData[0], "hh:mm:ss")];
          let currTimeIndex =
            _this.reverse_dict[_this.formatDateByProp(currData[0], "hh:mm:ss")];
          if (
            !(
              lastTimeIndex == currTimeIndex - 1 ||
              lastTimeIndex == currTimeIndex + 95
            )
          ) {
            let differ_num;
            if (lastTimeIndex > currTimeIndex) {
              differ_num = currTimeIndex + 96 - lastTimeIndex - 1;
            } else {
              differ_num = currTimeIndex - lastTimeIndex - 1;
            }
            let pressure_differ = (currData[1] - lastData[1]) / differ_num;
            let flow_differ = (currData[2] - lastData[2]) / differ_num;
            let temperature_differ = (currData[3] - lastData[3]) / differ_num;
            let res = [];
            for (let j = 1; j <= differ_num; j++) {
              res.push([
                _this.formatDateByProp(
                  new Date(lastData[0]).getTime() + 15 * 60 * 1000 * j
                ),
                lastData[1] + pressure_differ * j,
                lastData[2] + flow_differ * j,
                lastData[3] + temperature_differ * j,
              ]);
            }
            resultData.splice(i, 0, ...res);
          }
        }
        // for (let key in tableMap) {
        //   if (tableMap[key].length == 96 || key == "2023-10-07") {
        //     continue;
        //   }
        //   let lastWeek = _this.formatDateByProp(
        //     new Date(key).getTime() - 7 * 24 * 60 * 60 * 1000,
        //     "yyyy-MM-dd"
        //   );
        //   let lastWeekData = tableMap[lastWeek] || tableMap["2023-10-08"];
        //   let index = 0;
        //   while (tableMap[key].length != 96) {
        //     if (!tableMap[key][index]) {
        //       tableMap[key].splice(index, 0, [
        //         key + " " + _this.dict[index],
        //         lastWeekData[index][1],
        //         lastWeekData[index][2],
        //         lastWeekData[index][3],
        //       ]);
        //       index += 1;
        //       continue;
        //     }
        //     if (
        //       _this.formatDateByProp(tableMap[key][index][0], "hh:mm:ss") !=
        //       _this.dict[index]
        //     ) {
        //       tableMap[key].splice(index, 0, [
        //         key + " " + _this.dict[index],
        //         lastWeekData[index][1],
        //         lastWeekData[index][2],
        //         lastWeekData[index][3],
        //       ]);
        //     } else {
        //       index += 1;
        //     }
        //   }
        // }
        resultData.forEach((item, index) => {
          if (item[1] == 0) {
            item[1] = resultData[index - 1][1];
          }
          if (item[2] == 0) {
            item[2] = resultData[index - 1][2];
          }
          if (item[3] == 0) {
            item[3] = resultData[index - 1][3];
          }
          item[0] = _this.formatDateByProp(item[0]);
        });
        resultData.unshift(header);
        const resultWorkbook = XLSX.utils.book_new();
        const resultWorksheet = XLSX.utils.json_to_sheet(resultData);
        XLSX.utils.book_append_sheet(resultWorkbook, resultWorksheet, "Sheet1");
        XLSX.writeFile(resultWorkbook, "data_history_result.xlsx");
      };
      reader.readAsArrayBuffer(file);
    },
    formatDateByProp(date, fmt = "yyyy-MM-dd hh:mm:00") {
      date = new Date(date);
      let ret;
      const opt = {
        "[Yy]+": date.getFullYear().toString(), // 年
        "M+": (date.getMonth() + 1).toString(), // 月
        "[Dd]+": date.getDate().toString(), // 日
        "[Hh]+": date.getHours().toString(), // 时
        "m+": date.getMinutes().toString(), // 分
        "[Ss]+": date.getSeconds().toString(), // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
      };
      for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
          fmt = fmt.replace(
            ret[1],
            ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
          );
        }
      }
      return fmt;
    },
  },
};
</script>

<style></style>
